package gui.dialog;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.util.*;
import javax.swing.*;

import game.resources.*;
import gui.*;
import main.Antiquity;

/**
 *	@(#)TradeDialog.java
 *
 *	The <code>TradeDialog</code> class lets the players trade resources
 *	with each other or with the bank.
 *
 *	@author Grzegorz Kobiela
 *	@version 1.0 2008/5/14
 */
public final class TradeDialog
extends JDialog
{
	/** The trader's lists. */
	private ArrayList<JComboBox> traderLists;

	/** The player's lists. */
	private ArrayList<JComboBox> playerLists;

	/**
	 *	Creates a new <code>TradeDialog</code> instance.
	 *	@param owner the owner frame
	 *	@param player the player
	 *	@param trader the trade partner
	 */
	public TradeDialog(gui.GameWindow owner, final game.Player player, final game.Player trader) {
		super(owner, Antiquity.get("dlgTradeTitle"), true);
		traderLists = new ArrayList<JComboBox>();
		playerLists = new ArrayList<JComboBox>();
		String name = (trader != null ? trader.getName() : Antiquity.get("dlgTradeBank"));

		/* Dialog window */
		Container contentPane = getContentPane();
		contentPane.setLayout(null);
		contentPane.setBounds((Rectangle)Antiquity.getProperty("TradeDialogInnerBounds"));

		/* Player's panel */
		Rectangle bounds = new Rectangle((Rectangle)Antiquity.getProperty("TradeDialogPanelBounds"));
		JPanel panel = new JPanel(null);
		panel.setBounds(bounds);
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), player.getName()));
		ResourceType[] types = ResourceType.values();
		int[] amounts = new int[types.length];
		for(ResourceType type: types) amounts[type.ordinal()] = player.getResources(type).size();
		fillPanel(panel, playerLists, amounts);
		contentPane.add(panel);

		/* Trader's panel */
		Point offset = (Point)Antiquity.getProperty("TradeDialogPanelOffset");
		bounds.translate(offset.x, offset.y);
		panel = new JPanel(null);
		panel.setBounds(bounds);
		panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLoweredBevelBorder(), name));
		if(trader != null) {
			for(ResourceType type: types) amounts[type.ordinal()] = trader.getResources(type).size();
		} else {
			Arrays.fill(amounts, player.getResources().size() / 2);
		}
		fillPanel(panel, traderLists, amounts);
		contentPane.add(panel);

		/* Trade button */
		JButton button = new JButton(Antiquity.get("dlgTradeProceed"));
		button.setBounds((Rectangle)Antiquity.getProperty("TradeDialogButton1Bounds"));
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ResourceType[] types = ResourceType.values();
					int n = types.length;
					int[] pAmounts = new int[n];
					int[] tAmounts = new int[n];
					for(ResourceType type: types) {
						int i = type.ordinal();
						pAmounts[i] = (Integer)playerLists.get(i).getSelectedItem();
						tAmounts[i] = (Integer)traderLists.get(i).getSelectedItem();
					}
					Antiquity.trade(player, pAmounts, trader, tAmounts);
					setVisible(false);
					dispose();
				}
			}
		);
		contentPane.add(button);

		/* Trade arrows */
		String pColor = player.getColor().toString();
		String tColor = (trader != null ? trader.getColor() : game.PlayerColor.WOOD).toString();
		bounds = (Rectangle)Antiquity.getProperty("TradeDialogArrowBounds");
		BufferedImage img = new BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = img.createGraphics();
		g.drawImage(GameUtilities.getIcon("Arrow", pColor).getImage(), 0, 0, null);
		g.drawImage(ImageRotator.rotate(GameUtilities.getIcon("Arrow", tColor).getImage(), 180), 0, 0, null);
		JLabel label = new JLabel(new ImageIcon(img));
		label.setBounds(bounds);
		contentPane.add(label);

		/* Cancel button */
		button = new JButton(Antiquity.get("dlgTradeCancel"));
		button.setBounds((Rectangle)Antiquity.getProperty("TradeDialogButton2Bounds"));
		button.addActionListener(
			new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
					dispose();
				}
			}
		);
		contentPane.add(button);

		/* Dialog properties */
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		bounds = (Rectangle)Antiquity.getProperty("TradeDialogBounds");
		setLocation(
			owner.getX() + (owner.getWidth() - bounds.width) / 2,
			owner.getY() + (owner.getHeight() - bounds.height) / 2
		);
		setSize(bounds.getSize());
		setResizable(false);
		setVisible(true);
	}

	/**
	 *	Fills the given resource panel with resource labels
	 *	and drop-down lists containing the given numbers.
	 *	@param panel the panel
	 *	@param lists the drop-down lists
	 *	@param amounts the amounts of resources
	 */
	private void fillPanel(JPanel panel, ArrayList<JComboBox> lists, int[] amounts) {
		Rectangle bounds1 = new Rectangle((Rectangle)Antiquity.getProperty("TradeDialogLabelBounds"));
		Rectangle bounds2 = new Rectangle((Rectangle)Antiquity.getProperty("TradeDialogItemBounds"));
		Rectangle bounds3 = new Rectangle((Rectangle)Antiquity.getProperty("TradeDialogListBounds"));
		int step = (Integer)Antiquity.getProperty("TradeDialogItemStep");
		for(ResourceType type: ResourceType.values()) {
			int n = amounts[type.ordinal()];
			JLabel label = new JLabel(n + "\u00D7");
			label.setBounds(bounds1);
			panel.add(label);
			Resource resource = Resource.createResource(type);
			ImageIcon icon = GameUtilities.createIcon(resource);
			label = new JLabel(icon);
			label.setBounds(bounds2);
			label.setToolTipText(GameUtilities.getDescription(resource));
			panel.add(label);
			JComboBox list = new JComboBox();
			list.setBounds(bounds3);
			for(int i = 0; i <= n; i++) list.addItem(i);
			if(n == 0) list.setEnabled(false);
			panel.add(list);
			lists.add(list);
			bounds1.translate(0, step);
			bounds2.translate(0, step);
			bounds3.translate(0, step);
		}
	}
}